/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Guarda;
import co.edu.senaclem.modelo.Personal;
import static co.edu.senaclem.persistencia.Persistencia.areadb;
import static co.edu.senaclem.persistencia.Persistencia.con;
import static co.edu.senaclem.persistencia.Persistencia.conectar;
import static co.edu.senaclem.persistencia.Persistencia.desconectar;
import java.sql.ResultSet;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author anfeles
 */

@Stateless
public class GuardaDAO implements IGuardaDAO{
    public static ResultSet resSet=null;
    
    @Override
    public void eliminar(Guarda guarda) throws Exception {
        try {
            if (conectar()) {
                String consulta = "delete from guarda where documentoguarda='" +guarda.getDocumento().getDocumento()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Guarda guarda) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void insertar(Guarda guarda) throws Exception {
        try {
            if (conectar()) {
                String consulta = "insert into guarda values('" + guarda.getDocumento().getDocumento()+ "')";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Guarda consultarPorId(Long documento) throws Exception {
        try {
            Guarda miGuarda= null;
            if (conectar()) {
                String consulta = "Select * From guarda where documentoguarda='" + documento + "'";
                areadb = con.createStatement();
                resSet = areadb.executeQuery(consulta);

                if (resSet.next()) {
                    miGuarda = new Guarda();
                    //se consulta el personal asociado al guarda
                    IPersonalDAO personalDAO=new PersonalDAO();
                    Personal miPersonal=personalDAO.consultarPorId(resSet.getLong("documentoguarda"));                                        
                    miGuarda.setDocumento(miPersonal);                    
                }

                resSet.close();
                desconectar();
            }

            return miGuarda;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Guarda> consultar() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   
    
}
